﻿using Commonality.ReturnView;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Commonality.ServerEnum.AttributeEnum
{
    public static class ReadEnumRemark
    {
        public static Dictionary<string, Dictionary<int, string>> KeyEnumRemarkDC = new Dictionary<string, Dictionary<int, string>>();
        public static Dictionary<int, string> GetEnumRemarkDIC<T>()
        {

            if (KeyEnumRemarkDC.ContainsKey(typeof(T).Name))
            {
                KeyEnumRemarkDC.TryGetValue(typeof(T).Name, out var RemarkDC);
                return RemarkDC;
            }
            else
            {
                var dicKey = typeof(T).Name;
                Dictionary<int, string> EnumRemarkDC = new Dictionary<int, string>();
                var type = default(T).GetType();
                foreach (var fieldItem in type.GetFields())
                {
                    if (fieldItem.IsDefined(typeof(RemarkAttribute), true))
                    {
                        var remarkAttribute = fieldItem.GetCustomAttribute<RemarkAttribute>();
                        var remark = remarkAttribute.GetRemark();
                        EnumRemarkDC.Add(Convert.ToInt32(fieldItem.GetValue(fieldItem.Name)), remark);
                    }
                }
                KeyEnumRemarkDC.Add(dicKey, EnumRemarkDC);
                return EnumRemarkDC;
            }

        }
        public static IList<SelectListItem> ToSelectListByEnum(Type enumType, string selectdValue = "", string text = "请选择", string value = "")
        {
            IList<SelectListItem> listItem = new List<SelectListItem>();
            if (enumType.IsEnum)
            {
                if (!string.IsNullOrWhiteSpace(text))
                {
                    listItem.Add(new SelectListItem { Text = text, Value = value });
                }
                foreach (string enumText in Enum.GetNames(enumType))
                {
                    FieldInfo? field = enumType.GetField(enumText);
                    string remark = string.Empty;
                    if (field != null && field.IsDefined(typeof(RemarkAttribute), true))
                    {
                        object[] arr = field.GetCustomAttributes(typeof(RemarkAttribute), true);
                        remark = arr != null && arr.Length > 0 ? ((RemarkAttribute)arr[0]).GetRemark() : enumText;
                        SelectListItem selectListItem = new SelectListItem()
                        {
                            Value = ((int)Enum.Parse(enumType, enumText)).ToString(),
                            Text = remark,
                            Selected = false
                        };
                        if (selectListItem.Value.Equals(selectdValue))
                        {
                            selectListItem.Selected = true;
                        }
                        listItem.Add(selectListItem);
                    }
                }
            }
            else
            {
                throw new ArgumentException("请传入正确的枚举！");
            }
            return listItem;
        }
        public static string GetEnumRemark<T>(int enumType)
        {
            var type = default(T).GetType();
            var name = type.GetEnumName(enumType);
            var find = type.GetField(name);
            if (find.IsDefined(typeof(RemarkAttribute), true))
            {
                var remarkAttribute = find.GetCustomAttribute<RemarkAttribute>().GetRemark();
                return remarkAttribute;
            }
            else
            {
                return "枚举失败";
            }
        }
    }
}
